package com.lineage.server.model.Instance;

import com.lineage.server.model.L1Character;
import com.lineage.server.model.L1Object;
import com.lineage.server.model.map.L1Map;
import com.lineage.server.model.map.L1WorldMap;
import com.lineage.server.serverpackets.S_MoveCharPacket;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.types.Point;
import com.lineage.server.world.World;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class NpcMove extends NpcMoveExecutor {
  private static final Log _log = LogFactory.getLog(NpcMove.class);
  
  private final L1NpcInstance _npc;
  
  private int _error = 0;
  
	private int courceRange = 20;
  
  private final List<L1Character> extList = new ArrayList<>();
  
  private Iterator<int[]> _list = null;
  
  public NpcMove(L1NpcInstance npc) {
    this._npc = npc;
  }
  
//  public void setDirectionMove(int dir) {
//    if (!this._npc.isAiRunning())
//      this._npc.startAI(); 
//    if (dir >= 0) {
//      int locx = this._npc.getX();
//      int locy = this._npc.getY();
//      locx += HEADING_TABLE_X[dir];
//      locy += HEADING_TABLE_Y[dir];
//      this._npc.setHeading(dir);
//      if (!(this._npc instanceof L1DollInstance))
//        this._npc.getMap().setPassable((Point)this._npc.getLocation(), true); 
//      this._npc.broadcastPacketAll((ServerBasePacket)new S_MoveCharPacket(this._npc, locx, locy));
//      this._npc.setX(locx);
//      this._npc.setY(locy);
//      if (!(this._npc instanceof L1DollInstance))
//        this._npc.getMap().setPassable((Point)this._npc.getLocation(), false); 
//      if (this._npc.getMovementDistance() > 0 && (
//        this._npc instanceof L1GuardInstance || this._npc instanceof L1MerchantInstance || this._npc instanceof L1MonsterInstance) && 
//        this._npc.getLocation().getLineDistance(new Point(this._npc.getHomeX(), this._npc.getHomeY())) > this._npc.getMovementDistance())
//        this._npc.teleport(this._npc.getHomeX(), this._npc.getHomeY(), this._npc.getHeading()); 
//      if (this._npc.getNpcTemplate().get_npcId() >= 45912 && this._npc.getNpcTemplate().get_npcId() <= 45916 && (
//        this._npc.getX() < 32591 || this._npc.getX() > 32644 || this._npc.getY() < 32643 || this._npc.getY() > 32688 || this._npc.getMapId() != 4))
//        this._npc.teleport(this._npc.getHomeX(), this._npc.getHomeY(), this._npc.getHeading()); 
//    } 
//  }
  
	@Override
	public void setDirectionMove(final int dir) {
	    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
		if (dir >= 0) {								
			int locx = _npc.getX();//目前座标X
			int locy = _npc.getY();//目前座标Y					

			locx += HEADING_TABLE_X[dir];
			locy += HEADING_TABLE_Y[dir];
			
			_npc.setHeading(dir);

			if (!(_npc instanceof L1DollInstance) ) {
				// 解除旧座标障碍宣告
				this._npc.getMap().setPassable(_npc.getLocation(), true);
			}
			_npc.setX(locx);
			_npc.setY(locy);
			
			if (!(_npc instanceof L1DollInstance) ) {
				// 新增座标障碍宣告
				_npc.getMap().setPassable(_npc.getLocation(), false);
			}			
			
			//送出移动封包 动作编号0
			_npc.broadcastPacketAll(new S_MoveCharPacket(_npc));																	
		
			
			/**精准目标特效跟随*/
			ArrayList<L1EffectInstance> effectlist = _npc.get_TrueTargetEffectList();
			if (effectlist != null) {//身上具有精准目标特效
				for (L1EffectInstance effect : effectlist) {
					effect.Follow(_npc);
				}
			}
			
			// movement_distancet 超过最大移动距离
			if (_npc.getMovementDistance() > 0) {
				if ((_npc instanceof L1GuardInstance)
						|| (_npc instanceof L1MerchantInstance)
						|| (_npc instanceof L1MonsterInstance)) {
					if (_npc.getLocation().getLineDistance(
							new Point(_npc.getHomeX(), _npc.getHomeY())) > _npc.getMovementDistance()) {
						_npc.teleport(_npc.getHomeX(), _npc.getHomeY(), _npc.getHeading());
					}
				}
			}	
			
			// 古鲁丁墓园怪物
			if ((_npc.getNpcTemplate().get_npcId() >= 45912)
					&& (_npc.getNpcTemplate().get_npcId() <= 45916)) {
				if ((_npc.getX() < 32591 || _npc.getX() > 32644)
						&& (_npc.getY() < 32643 || _npc.getY() > 32688)
						&& (_npc.getMapId() == 4)) {
					_npc.teleport(_npc.getHomeX(), _npc.getHomeY(), _npc.getHeading());
				}
			}
		}
	}
  
  public void clear() {
    if (this._list != null)
      this._list = null; 
    this.extList.clear();
  }
  
  public int moveDirection(int x, int y) {
    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
    int dir = 0;
    try {
      double d = this._npc.getLocation().getLineDistance(new Point(x, y));
      if (this._npc.hasSkillEffect(40) && d >= 2.0D)
        return -1; 
      if (d > 30.0D)
        return -1; 
      if (d > 20.0D) {
        dir = targetDirection(x, y);
        dir = checkObject(dir);
        dir = openDoor(dir);
      } else {
        dir = _serchCource(x, y);
        if (dir == -1) {
          dir = targetDirection(x, y);
          dir = checkObject(dir);
          dir = openDoor(dir);
          if (!(this._npc instanceof L1SummonInstance) && !(this._npc instanceof L1PetInstance) && 
            dir == -1 && this._npc.is_now_target() != null) {
            this._error++;
            if (this._error >= 1) {
              Collection<L1Object> allObj = World.get().getVisibleObjects((L1Object)this._npc, 1);
              if (allObj.isEmpty())
                return dir; 
              for (Iterator<L1Object> iter = allObj.iterator(); iter.hasNext(); ) {
                L1Object obj = iter.next();
                if (obj instanceof L1Character) {
                  L1Character cha = (L1Character)obj;
                  if ((!(cha instanceof L1PcInstance) && !(cha instanceof L1SummonInstance) && !(cha instanceof L1PetInstance)) || 
                    cha.isDead())
                    continue; 
                  if (this.extList.contains(cha))
                    continue; 
                  this.extList.add(cha);
                  this._npc._hateList.add(cha, 0);
                  this._npc.checkTarget();
                  this._npc.setNowTarget(cha);
                  this._error = 0;
                  break;
                } 
              } 
            } 
          } 
        } 
      } 
    } catch (Exception e) {
      _log.error(e.getLocalizedMessage(), e);
    } 
    return dir;
  }
  
  public int targetReverseDirection(int tx, int ty) {
    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
    int dir = targetDirection(tx, ty);
    return HEADING_RD[dir];
  }
  
  private int targetDirection(int tx, int ty) {
    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
    float dis_x = Math.abs(this._npc.getX() - tx);
    float dis_y = Math.abs(this._npc.getY() - ty);
    float dis = Math.max(dis_x, dis_y);
    if (dis == 0.0F)
      return this._npc.getHeading(); 
    int avg_x = (int)Math.floor((dis_x / dis + 0.59F));
    int avg_y = (int)Math.floor((dis_y / dis + 0.59F));
    int dir_x = 0;
    int dir_y = 0;
    if (this._npc.getX() < tx)
      dir_x = 1; 
    if (this._npc.getX() > tx)
      dir_x = -1; 
    if (this._npc.getY() < ty)
      dir_y = 1; 
    if (this._npc.getY() > ty)
      dir_y = -1; 
    if (avg_x == 0)
      dir_x = 0; 
    if (avg_y == 0)
      dir_y = 0; 
    if (dir_x == 1 && dir_y == -1)
      return 1; 
    if (dir_x == 1 && dir_y == 0)
      return 2; 
    if (dir_x == 1 && dir_y == 1)
      return 3; 
    if (dir_x == 0 && dir_y == 1)
      return 4; 
    if (dir_x == -1 && dir_y == 1)
      return 5; 
    if (dir_x == -1 && dir_y == 0)
      return 6; 
    if (dir_x == -1 && dir_y == -1)
      return 7; 
    if (dir_x == 0 && dir_y == -1)
      return 0; 
    return this._npc.getHeading();
  }
  
  private boolean glanceCheck(int tx, int ty, int th) {
    L1Map map = this._npc.getMap();
    int chx = this._npc.getX();
    int chy = this._npc.getY();
    for (int i = 0; i < 15 && (
      chx != tx || chy != ty) && (chx + 1 != tx || chy - 1 != ty) && (chx + 1 != tx || chy != ty) && (chx + 1 != tx || chy + 1 != ty) && (
      chx != tx || chy + 1 != ty) && (chx - 1 != tx || chy + 1 != ty) && (chx - 1 != tx || chy != ty) && (chx - 1 != tx || chy - 1 != ty) && (
      chx != tx || chy - 1 != ty); i++) {
      if (!map.isPassable(chx, chy, th, this._npc))
        return false; 
      if (map.isExistDoor(chx, chy) == 3)
        return false; 
      if (chx < tx) {
        if (chy == ty) {
          chx++;
        } else if (chy > ty) {
          chx++;
          chy--;
        } else if (chy < ty) {
          chx++;
          chy++;
        } 
      } else if (chx == tx) {
        if (chy < ty) {
          chy++;
        } else if (chy > ty) {
          chy--;
        } 
      } else if (chx > tx) {
        if (chy == ty) {
          chx--;
        } else if (chy < ty) {
          chx--;
          chy++;
        } else if (chy > ty) {
          chx--;
          chy--;
        } 
      } 
    } 
    return true;
  }
  
  public int checkObject(int h) {
    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
    L1Map map = L1WorldMap.get().getMap(this._npc.getMapId());
    int x = this._npc.getX();
    int y = this._npc.getY();
    if (h == 1) {
      if (map.isPassable(x, y, 1, null))
        return 1; 
      if (map.isPassable(x, y, 0, null))
        return 0; 
      if (map.isPassable(x, y, 2, null))
        return 2; 
    } else if (h == 2) {
      if (map.isPassable(x, y, 2, null))
        return 2; 
      if (map.isPassable(x, y, 1, null))
        return 1; 
      if (map.isPassable(x, y, 3, null))
        return 3; 
    } else if (h == 3) {
      if (map.isPassable(x, y, 3, null))
        return 3; 
      if (map.isPassable(x, y, 2, null))
        return 2; 
      if (map.isPassable(x, y, 4, null))
        return 4; 
    } else if (h == 4) {
      if (map.isPassable(x, y, 4, null))
        return 4; 
      if (map.isPassable(x, y, 3, null))
        return 3; 
      if (map.isPassable(x, y, 5, null))
        return 5; 
    } else if (h == 5) {
      if (map.isPassable(x, y, 5, null))
        return 5; 
      if (map.isPassable(x, y, 4, null))
        return 4; 
      if (map.isPassable(x, y, 6, null))
        return 6; 
    } else if (h == 6) {
      if (map.isPassable(x, y, 6, null))
        return 6; 
      if (map.isPassable(x, y, 5, null))
        return 5; 
      if (map.isPassable(x, y, 7, null))
        return 7; 
    } else if (h == 7) {
      if (map.isPassable(x, y, 7, null))
        return 7; 
      if (map.isPassable(x, y, 6, null))
        return 6; 
      if (map.isPassable(x, y, 0, null))
        return 0; 
    } else if (h == 0) {
      if (map.isPassable(x, y, 0, null))
        return 0; 
      if (map.isPassable(x, y, 7, null))
        return 7; 
      if (map.isPassable(x, y, 1, null))
        return 1; 
    } 
    return -1;
  }
  
  public int openDoor(int h) {
    if (!this._npc.isAiRunning())
      this._npc.startAI(); 
    if (h != -1 && 
      !this._npc.getMap().isDoorPassable(this._npc.getX(), this._npc.getY(), h, this._npc))
      return -1; 
    return h;
  }
  
  private void _moveLocation(int[] ary, int d) {
    switch (d) {
      case 1:
        ary[0] = ary[0] + 1;
        ary[1] = ary[1] - 1;
        break;
      case 2:
        ary[0] = ary[0] + 1;
        break;
      case 3:
        ary[0] = ary[0] + 1;
        ary[1] = ary[1] + 1;
        break;
      case 4:
        ary[1] = ary[1] + 1;
        break;
      case 5:
        ary[0] = ary[0] - 1;
        ary[1] = ary[1] + 1;
        break;
      case 6:
        ary[0] = ary[0] - 1;
        break;
      case 7:
        ary[0] = ary[0] - 1;
        ary[1] = ary[1] - 1;
        break;
      case 0:
        ary[1] = ary[1] - 1;
        break;
    } 
    ary[2] = d;
  }
  
  private void _getFront(int[] ary, int d) {
    switch (d) {
      case 1:
        ary[4] = 2;
        ary[3] = 0;
        ary[2] = 1;
        ary[1] = 3;
        ary[0] = 7;
        break;
      case 2:
        ary[4] = 2;
        ary[3] = 4;
        ary[2] = 0;
        ary[1] = 1;
        ary[0] = 3;
        break;
      case 3:
        ary[4] = 2;
        ary[3] = 4;
        ary[2] = 1;
        ary[1] = 3;
        ary[0] = 5;
        break;
      case 4:
        ary[4] = 2;
        ary[3] = 4;
        ary[2] = 6;
        ary[1] = 3;
        ary[0] = 5;
        break;
      case 5:
        ary[4] = 4;
        ary[3] = 6;
        ary[2] = 3;
        ary[1] = 5;
        ary[0] = 7;
        break;
      case 6:
        ary[4] = 4;
        ary[3] = 6;
        ary[2] = 0;
        ary[1] = 5;
        ary[0] = 7;
        break;
      case 7:
        ary[4] = 6;
        ary[3] = 0;
        ary[2] = 1;
        ary[1] = 5;
        ary[0] = 7;
        break;
      case 0:
        ary[4] = 2;
        ary[3] = 6;
        ary[2] = 0;
        ary[1] = 1;
        ary[0] = 7;
        break;
    } 
  }
  
//  private int _serchCource(int x, int y) {
//    if (!this._npc.isAiRunning())
//      this._npc.startAI(); 
//    int locCenter = 21;
//    int diff_x = x - 21;
//    int diff_y = y - 21;
//    int[] locBace = { this._npc.getX() - diff_x, this._npc.getY() - diff_y };
//    int[] locNext = new int[4];
//    int[] dirFront = new int[5];
//    boolean[][] serchMap = new boolean[43][43];
//    LinkedList<int[]> queueSerch = (LinkedList)new LinkedList<>();
//    for (int j = 41; j > 0; j--) {
//      for (int k = 20 - Math.abs(21 - j); k >= 0; k--) {
//        serchMap[j][21 + k] = true;
//        serchMap[j][21 - k] = true;
//      } 
//    } 
//    int[] firstCource = { 2, 4, 6, 0, 1, 3, 5, 7 };
//    int i;
//    for (i = 0; i < 8; i++) {
//      System.arraycopy(locBace, 0, locNext, 0, 4);
//      _moveLocation(locNext, firstCource[i]);
//      if (locNext[0] - 21 == 0 && locNext[1] - 21 == 0)
//        return firstCource[i]; 
//      if (serchMap[locNext[0]][locNext[1]]) {
//        int tmpX = locNext[0] + diff_x;
//        int tmpY = locNext[1] + diff_y;
//        boolean found = false;
//        switch (i) {
//          case 0:
//            found = this._npc.getMap().isPassable(tmpX, tmpY + 1, i, this._npc);
//            break;
//          case 1:
//            found = this._npc.getMap().isPassable(tmpX - 1, tmpY + 1, i, this._npc);
//            break;
//          case 2:
//            found = this._npc.getMap().isPassable(tmpX - 1, tmpY, i, this._npc);
//            break;
//          case 3:
//            found = this._npc.getMap().isPassable(tmpX - 1, tmpY - 1, i, this._npc);
//            break;
//          case 4:
//            found = this._npc.getMap().isPassable(tmpX, tmpY - 1, i, this._npc);
//            break;
//          case 5:
//            found = this._npc.getMap().isPassable(tmpX + 1, tmpY - 1, i, this._npc);
//            break;
//          case 6:
//            found = this._npc.getMap().isPassable(tmpX + 1, tmpY, i, this._npc);
//            break;
//          case 7:
//            found = this._npc.getMap().isPassable(tmpX + 1, tmpY + 1, i, this._npc);
//            break;
//        } 
//        if (found) {
//          int[] locCopy = new int[4];
//          System.arraycopy(locNext, 0, locCopy, 0, 4);
//          locCopy[2] = firstCource[i];
//          locCopy[3] = firstCource[i];
//          queueSerch.add(locCopy);
//        } 
//        serchMap[locNext[0]][locNext[1]] = false;
//      } 
//    } 
//    locBace = null;
//    while (queueSerch.size() > 0) {
//      locBace = queueSerch.removeFirst();
//      _getFront(dirFront, locBace[2]);
//      for (i = 4; i >= 0; i--) {
//        System.arraycopy(locBace, 0, locNext, 0, 4);
//        _moveLocation(locNext, dirFront[i]);
//        if (locNext[0] - 21 == 0 && locNext[1] - 21 == 0)
//          return locNext[3]; 
//        if (serchMap[locNext[0]][locNext[1]]) {
//          int tmpX = locNext[0] + diff_x;
//          int tmpY = locNext[1] + diff_y;
//          boolean found = false;
//          switch (i) {
//            case 0:
//              found = this._npc.getMap().isPassable(tmpX, tmpY + 1, i, this._npc);
//              break;
//            case 1:
//              found = this._npc.getMap().isPassable(tmpX - 1, tmpY + 1, i, this._npc);
//              break;
//            case 2:
//              found = this._npc.getMap().isPassable(tmpX - 1, tmpY, i, this._npc);
//              break;
//            case 3:
//              found = this._npc.getMap().isPassable(tmpX - 1, tmpY - 1, i, this._npc);
//              break;
//            case 4:
//              found = this._npc.getMap().isPassable(tmpX, tmpY - 1, i, this._npc);
//              break;
//          } 
//          if (found) {
//            int[] locCopy = new int[4];
//            System.arraycopy(locNext, 0, locCopy, 0, 4);
//            locCopy[2] = dirFront[i];
//            queueSerch.add(locCopy);
//          } 
//          serchMap[locNext[0]][locNext[1]] = false;
//        } 
//      } 
//      locBace = null;
//    } 
//    return -1;
//  }
  
	private int _serchCource(final int x, final int y) { // 目标点Ｘ 目标点Ｙ
	    int i;
		final int locCenter = courceRange + 1;
	    final int diff_x = x - locCenter; // Ｘの实际のロケーションとの差
	    final int diff_y = y - locCenter; // Ｙの实际のロケーションとの差
	    int[] locBace = { _npc.getX() - diff_x, _npc.getY() - diff_y, 0, 0 }; // Ｘ
	                                                                          // Ｙ
	    // 方向
	    // 初期方向
	    final int[] locNext = new int[4];
	    int[] locCopy;
	    final int[] dirFront = new int[5];
	    final boolean serchMap[][] = new boolean[locCenter * 2 + 1][locCenter * 2 + 1];
	    final LinkedList<int[]> queueSerch = new LinkedList<int[]>();

	    // 设置探索地图
		for (int j = courceRange * 2 + 1; j > 0; j--) {
			for (i = courceRange - Math.abs(locCenter - j); i >= 0; i--) {
                serchMap[j][locCenter + i] = true;
                serchMap[j][locCenter - i] = true;
	        }
	    }

	    // 初期方向の设置
	    final int[] firstCource = { 2, 4, 6, 0, 1, 3, 5, 7 };
	    for (i = 0; i < 8; i++) {
	        System.arraycopy(locBace, 0, locNext, 0, 4);
	        this._moveLocation(locNext, firstCource[i]);
	        if ((locNext[0] - locCenter == 0) && (locNext[1] - locCenter == 0)) {
		        // 最短经路が见つかった场合:邻
		        return firstCource[i];
	        }
	        if (serchMap[locNext[0]][locNext[1]]) {
                final int tmpX = locNext[0] + diff_x;
                final int tmpY = locNext[1] + diff_y;
                boolean found = false;
                /*switch (i) {
                case 0:
                    found = _npc.getMap().isPassableDna(tmpX, tmpY + 1, i);
                    break;

                case 1:
                    found = _npc.getMap().isPassableDna(tmpX - 1, tmpY + 1, i);
                    break;

                case 2:
                    found = _npc.getMap().isPassableDna(tmpX - 1, tmpY, i);
                    break;

                case 3:
                    found = _npc.getMap().isPassableDna(tmpX - 1, tmpY - 1, i);
                    break;

                case 4:
                    found = _npc.getMap().isPassableDna(tmpX, tmpY - 1, i);
                    break;

                case 5:
                    found = _npc.getMap().isPassableDna(tmpX + 1, tmpY - 1, i);
                    break;

                case 6:
                    found = _npc.getMap().isPassableDna(tmpX + 1, tmpY, i);
                    break;

                case 7:
                    found = _npc.getMap().isPassableDna(tmpX + 1, tmpY + 1, i);
                    break;
                }*/
				if (i == 0) {
					found = _npc.getMap().isPassable(tmpX, tmpY + 1, i);
				} else if (i == 1) {
					found = _npc.getMap().isPassable(tmpX - 1, tmpY + 1, i);
				} else if (i == 2) {
					found = _npc.getMap().isPassable(tmpX - 1, tmpY, i);
				} else if (i == 3) {
					found = _npc.getMap().isPassable(tmpX - 1, tmpY - 1, i);
				} else if (i == 4) {
					found = _npc.getMap().isPassable(tmpX, tmpY - 1, i);
				} else if (i == 5) {
					found = _npc.getMap().isPassable(tmpX + 1, tmpY - 1, i);
				} else if (i == 6) {
					found = _npc.getMap().isPassable(tmpX + 1, tmpY, i);
				} else if (i == 7) {
					found = _npc.getMap().isPassable(tmpX + 1, tmpY + 1, i);
				}
                if (found) {// 移动经路があった场合
                    locCopy = new int[4];
                    System.arraycopy(locNext, 0, locCopy, 0, 4);
                    locCopy[2] = firstCource[i];
                    locCopy[3] = firstCource[i];
                    queueSerch.add(locCopy);
                }
                serchMap[locNext[0]][locNext[1]] = false;
	        }
	    }
	    locBace = null;

        // 最短经路を探索
        while (queueSerch.size() > 0) {
            locBace = queueSerch.removeFirst();
            this._getFront(dirFront, locBace[2]);
            for (i = 4; i >= 0; i--) {
                System.arraycopy(locBace, 0, locNext, 0, 4);
                this._moveLocation(locNext, dirFront[i]);
                if ((locNext[0] - locCenter == 0)
                        && (locNext[1] - locCenter == 0)) {
                    return locNext[3];
                }
                if (serchMap[locNext[0]][locNext[1]]) {
                    final int tmpX = locNext[0] + diff_x;
                    final int tmpY = locNext[1] + diff_y;
                    boolean found = false;
                    /*switch (i) {
                        case 0:
                            found = _npc.getMap().isPassableDna(tmpX, tmpY + 1, i);
                            break;

                        case 1:
                            found = _npc.getMap().isPassableDna(tmpX - 1,
                                    tmpY + 1, i);
                            break;

                        case 2:
                            found = _npc.getMap().isPassableDna(tmpX - 1, tmpY, i);
                            break;

                        case 3:
                            found = _npc.getMap().isPassableDna(tmpX - 1,
                                    tmpY - 1, i);
                            break;

                        case 4:
                            found = _npc.getMap().isPassableDna(tmpX, tmpY - 1, i);
                            break;
                    }*/
					if (i == 0) {
						found = _npc.getMap().isPassable(tmpX, tmpY + 1, i);
					} else if (i == 1) {
						found = _npc.getMap().isPassable(tmpX - 1, tmpY + 1, i);
					} else if (i == 2) {
						found = _npc.getMap().isPassable(tmpX - 1, tmpY, i);
					} else if (i == 3) {
						found = _npc.getMap().isPassable(tmpX - 1, tmpY - 1, i);
					} else if (i == 4) {
						found = _npc.getMap().isPassable(tmpX, tmpY - 1, i);
					}
                    if (found) { // 移动经路があった场合
                        locCopy = new int[4];
                        System.arraycopy(locNext, 0, locCopy, 0, 4);
                        locCopy[2] = dirFront[i];
                        queueSerch.add(locCopy);
                    }
                    serchMap[locNext[0]][locNext[1]] = false;
                }
            }
            locBace = null;
        }
        return -1; // 目标までの经路がない场合
    }
}
